package neu.competition.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import neu.competition.dto.MatchesDTO;
import neu.competition.entity.EntryRecords;
import neu.competition.entity.Matches;
import neu.competition.entity.MembershipType;
import neu.competition.mapper.EntryRecordsMapper;
import neu.competition.mapper.MatchesMapper;
import neu.competition.mapper.MembershipTypeMapper;
import neu.competition.service.MatchesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MatchesServiceImpl implements MatchesService {

    @Autowired
    MembershipTypeMapper mtm;
    @Autowired
    private MatchesMapper matchesMapper;
    @Autowired
    private EntryRecordsMapper erm;

    /**
     * 我的比赛
     *
     * @param id
     * @return
     */
    public List<MatchesDTO> getMatchesListByUserId(String id) {
        QueryWrapper<Matches> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("managerId", id);
        List<Matches> matchesList = matchesMapper.selectList(queryWrapper);
        List<MatchesDTO> matchesDTOS = MatchesDTOList(matchesList);
        return matchesDTOS;
    }

    /**
     * 新增比赛
     *
     * @param matchesDTO
     */
    public int insert(MatchesDTO matchesDTO, String id) {
        Matches matches = new Matches();
        BeanUtils.copyProperties(matchesDTO, matches);

        matches.setManagerid(id);
        matches.setStatecompetition(0);
        matchesMapper.insert(matches);
        return matches.getMatchid();
    }

    @Override
    public MatchesDTO getMatchById(Integer id) {
        Matches matches = matchesMapper.selectById(id);
        MatchesDTO matchesDTO = new MatchesDTO();
        if (matches == null) {
            matches = new Matches();
            matches.setMatchid(-1);
        }
        BeanUtils.copyProperties(matches, matchesDTO);

        return matchesDTO;
    }

    @Override
    public int updateMatch(MatchesDTO matchesDTO) {
        Matches matches = new Matches();
        BeanUtils.copyProperties(matchesDTO, matches);

        return matchesMapper.updateById(matches);
    }

    @Override
    public List<MatchesDTO> getMatchListByComId(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comid", id);
        List<Matches> matchesList = matchesMapper.selectList(queryWrapper);
        List<MatchesDTO> matchesDTOS=MatchesDTOList(matchesList);
        return matchesDTOS;
    }

    @Override
    public int matchUpdateStateCompetition(Integer id) {
        LambdaUpdateWrapper<Matches> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Matches::getMatchid, id) // 设置条件，例如 id = 1
                .set(Matches::getStatecompetition, 1);
        int update = matchesMapper.update(null, updateWrapper);
        return update;
    }

    public List<MatchesDTO> MatchesDTOList(List<Matches> matchesList) {
        return matchesList.stream().map(matches -> {
            MatchesDTO dto = new MatchesDTO();
            BeanUtils.copyProperties(matches, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    /*
    查询所有比赛
    */
    public List<MatchesDTO> getAllMatchesList() {
        List<Matches> matchesList = matchesMapper.selectList(null);
        List<MatchesDTO> matchesDTOS = MatchesDTOList(matchesList);
        return matchesDTOS;
    }

    public boolean isSignedup(int mid, int cid, String uid) {
        QueryWrapper<MembershipType> qw = new QueryWrapper<>();
        qw.eq("userid", uid);
        QueryWrapper<EntryRecords> qw1 = new QueryWrapper<>();
        List<MembershipType> ml = mtm.selectList(qw);
        for (MembershipType m : ml) {
            int tid = m.getTeamid();
            qw1.eq("matchid", mid);
            qw1.eq("comid", cid);
            qw1.eq("teamid", tid);
            if (erm.exists(qw1))
                return true;
            qw1.clear();
        }
        return false;
    }

    public boolean SignUp(int mid, int cid, int teamid) {
        System.out.println(mid);
        System.out.println(cid);
        System.out.println(teamid);
        EntryRecords er = new EntryRecords();
        er.setMatchid(mid);
        er.setTeamid(teamid);
        er.setComid(cid);
        QueryWrapper<Matches> qw = new QueryWrapper<>();
        qw.eq("matchid", mid);
        er.setAwards(matchesMapper.selectOne(qw).getAwardintroduction());
        return erm.insert(er) > 0;
    }
}
